package com.jsoft.controller;

import com.alibaba.fastjson.JSON;
import com.jsoft.entity.User;
import com.jsoft.service.UserService;
import com.jsoft.service.impl.UserServiceImpl;
import com.jsoft.util.Message;
import com.jsoft.util.ResultUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author:sha
 * @email:o68o4741@163.com
 * @date:2023/2/20
 */
@WebServlet("*.do")
@MultipartConfig
public class LoginController extends HttpServlet {
    private static UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  {
        String servletPath = request.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 3);
        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        User userDb = new User();
        userDb.setAccount(account);
        userDb.setPassword(password);
        if (account == null || account.equals("")) {
            out.write(JSON.toJSONString(new Message(202, "账号为空。失败，请重试！")));
            return;
        } else if (password == null || password.equals("")) {
            out.write(JSON.toJSONString(new Message(203, "密码为空。失败，请重试！")));
            return;
        }
        try {
            User userByAccount = userService.getUserByAccount(account);
            if(userByAccount == null){

                out.write(JSON.toJSONString(new Message(204, "用户名不存在，请重试...")));
                return;
            }
            boolean login = userService.login(userDb);
            Integer enterpriseId = userService.getUserByAccount(account).getEnterpriseId();
            if (login) {
                out.write(JSON.toJSONString(new Message(200, "登录成功！")));
            } else {
                out.write(JSON.toJSONString(new Message(201, "登录失败!")));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            out.close();
        }
    }
    protected void findEnterpriseId(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String account = request.getParameter("account");
        try {
            User userByAccount = userService.getUserByAccount(account);
            Integer enterpriseId = userByAccount.getEnterpriseId();
            out.write(JSON.toJSONString(new ResultUtil("enterpriseId",enterpriseId)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    protected void getUserName(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String account = request.getParameter("account");
        try {
            User userByAccount = userService.getUserByAccount(account);
            String userName = userByAccount.getName();
            out.write(JSON.toJSONString(new ResultUtil("name",userName)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        doGet(request, response);
    }
}
